import { useGlobalStore } from '@/store/global'
import { useInformationStore } from '@/store/information'
import { useUserStore } from '@/store/user'
import { storeToRefs } from 'pinia'
import { watch } from 'vue'
import { useI18n } from 'vue-i18n'
import { useToken } from './token'

export function useGlobalData() {
  const { setCategories } = useInformationStore()
  const { setAdvertise } = useGlobalStore()
  const userStore = useUserStore()
  const { refreshToken } = useToken()
  const { isLogin } = storeToRefs(userStore)
  const { setUserInfo } = userStore
  const { locale } = useI18n()
  setCategories()
  setAdvertise()
  watch(isLogin, (val) => {
    if (val) {
      setUserInfo()
      refreshToken()
    }
  }, { immediate: true })
  watch(locale, () => {
    location.reload()
  })
}
